package com.example.arithmeticleetcode.leetcode;

import org.springframework.util.Base64Utils;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;
import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-06-17 10:27
 **/
public class Demo30 {

    public static void main(String[] args) throws Exception {
        int[] arr = {5, 5, 4};
        int k = 1;
        System.out.println(findLeastNumOfUniqueInts(arr, k));

        int[] arr1 = {4, 3, 1, 1, 3, 3, 2};
        int k1 = 3;
        System.out.println(findLeastNumOfUniqueInts(arr1, k1));

        int[] arr2 = {2, 4, 1, 8, 3, 5, 1, 3};
        int k2 = 3;
        System.out.println(findLeastNumOfUniqueInts(arr2, k2));

        Long timestamp = System.currentTimeMillis();
        String secret = "SEC0accf6229b8362e3d8d421bdd0195473cfe98958ce1fab88b83fc127e891bc78";

        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        System.out.println(Base64Utils.encodeToString(signData));
        String sign = URLEncoder.encode(new String(Base64Utils.encodeToString(signData)), "UTF-8");
        System.out.println(sign);
    }

    public static int findLeastNumOfUniqueInts(int[] arr, int k) {
        Map<Integer, Integer> map = new HashMap<>(arr.length);
        for (int i : arr) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }
        ArrayList<Integer> list = new ArrayList<>(map.values());
//        Collection<Integer> values = map.values();
//        for (Integer integer : map.values()) {
//            list.add(integer);
//        }
        Collections.sort(list);
        int count = 0;
        int sum = 0;
        for (Integer value : list) {
            sum += value;
            if (k >= sum) {
                continue;
            }
            count++;
        }
        return count;
    }
}
